<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8" />
  <meta name="author" content="wangbo" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>数组</title>
</head>

<body>
  <script>



    // 将所有的数据 遍历一遍
    let arr = [10, 20, 30, 40, 10, 20, 30, 1, 2, 30]
    //将数组中 所有的小于 30的数据,放到新 数组里边
    // let xin = [10, 20,10, 20,1, 2,]
    let xin = []
    // 需要将数组中,每一个数据拿出来,判断是不是<30 放到新数组里边

    // console.log(arr[0]);
    // i == arr.length - 1
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] % 2 == 0) {
        xin.push(arr[i])
      }
    }

    let myFIlter = function (arr, fn) {
      let newArr = []
      for (let i = 0; i < arr.length; i++) {
        // 函数需要返回true ||false
        if (fn(arr[i])) {
          newArr.push(arr[i])
        }
      }
      return newArr
    }

    // 使用函数,来替换咱们代码,使myFIlter 通用性增大
    let aa = myFIlter(arr, function (item) { return item < 5 })



    let arr = [10, 20, 100, 3, 2000]

    let myforeach = function (arr, fn) {
      for (let i = 0; i < arr.length; i++) {
        // console.log(arr[i],i);
        fn(arr[i],i)
        // console.log("我是数组里边的值", arr[i]);
      }
    }
    myforeach(arr, function (item,i) { console.log("我是数组里边的值", item,i) })
    
    myforeach(arr, (item,i) => {
      console.log("我是数组里边的值", item,i)
    } )

    arr.forEach((item,i) => {
      console.log("我是数组里边的值", item,i)
    })


    let arr = [10, 2, 100, 3, 2000]
    //  找到 第一个 >15的 元素的下标,
    let myfinde = function (arr) {
      // 判断第一个
      for (let i = 0; i < arr.length; i++) {
        if (arr[i] > 100) {
          return i
        }
      }
    }

    let myfinde222 = function (arr, fn) {
      // 判断第一个
      for (let i = 0; i < arr.length; i++) {
        if (fn(arr[i])) {
          return i
        }
      }
    }

    let c = myfinde(arr)
    let d = myfinde222(arr, function (item) { return item > 100 })
    console.log(c, d);



    console.log(aa);
    let students = [
      { name: '小明', age: 18, gender: "男", hometown: '河北省' },
      { name: '小红', age: 19, gender: "女", hometown: '河南省' },
      { name: '小刚', age: 17, gender: "男", hometown: '山西省' },
      { name: '小丽', age: 18, gender: "女", hometown: '山东省' },
    ]
    // 将  对象里边 所有的 name 属性值 ,返回成 数组 
    // ["小明", '小红', '小刚', '小丽']
    let mymap = function (arr, fn) {
      let xin = []

      for (let i = 0; i < arr.length; i++) {
        const obj = arr[i]
        let item = fn(obj)
        xin.push(item)
      }
      return xin
      console.log(xin);
    }
    // let c =  mymap(students)
    let c = mymap(students, function (obj) {
      return obj.name[1]
    })
    console.log(c);

    let d = students.map(function (item) {
      return item.name[1]
    })
    console.log(d);




  </script>
</body>

</html>